﻿using System;
using System.Collections.Generic;
using System.IO;
using ProjScan.Modules;
using ProjScan.Submodules.Shader_Analysis.Build;
using ProjScan.Tools;
using UnityEditor;
using UnityEngine;

namespace ProjScan.Submodules.Shader_Analysis
{
	// Token: 0x0200017D RID: 381
	internal class ShaderCompilationManager : Singleton<ShaderCompilationManager>
	{
		// Token: 0x06000D51 RID: 3409 RVA: 0x00006CD6 File Offset: 0x00004ED6
		public void RegisterProcessShader()
		{
			ApiCompatibilityUtils.Instance.RegisterProcessShader(new Action<Shader, UPS_SSD, List<UPS_SCD>>(this.CompileShader));
		}

		// Token: 0x06000D52 RID: 3410 RVA: 0x0003B47C File Offset: 0x0003967C
		public void CompileShader(Shader shader, UPS_SSD ups_SSD, IList<UPS_SCD> ups_SCDList)
		{
			bool shouldPrepareBuildRules = Singleton<ShaderAnalysisCheckModule>.Instance.shouldPrepareBuildRules;
			if (shouldPrepareBuildRules)
			{
				try
				{
					ShaderBuildInfo shaderBuildInfo = Singleton<ShaderInfoManager>.Instance.GetShaderInfo(shader.name);
					bool flag = shaderBuildInfo == null;
					if (flag)
					{
						shaderBuildInfo = new ShaderBuildInfo();
						shaderBuildInfo.name = shader.name;
						shaderBuildInfo.simpleName = ShaderPassKeyManager.GetShaderName(shaderBuildInfo.name);
						string assetPath = AssetDatabase.GetAssetPath(shader);
						shaderBuildInfo.isInternal = (string.IsNullOrEmpty(assetPath) || !File.Exists(assetPath));
						bool flag2 = !shaderBuildInfo.isInternal;
						if (flag2)
						{
							shaderBuildInfo.path = assetPath;
						}
						Singleton<ShaderInfoManager>.Instance.AddShaderBuildInfo(shaderBuildInfo);
					}
					int count = ups_SCDList.Count;
					bool flag3 = ups_SSD.shaderType == UPS_SSD.UPS_ShaderType.Vertex;
					if (flag3)
					{
						shaderBuildInfo.AddPassData(count);
					}
					for (int i = 0; i < count; i++)
					{
						foreach (string text in ups_SCDList[i].shaderKeywordSet)
						{
							bool flag4 = !string.IsNullOrEmpty(text) && !shaderBuildInfo.ContainsKeyword(text, true);
							if (flag4)
							{
								shaderBuildInfo.AddKeyword(text);
							}
						}
					}
				}
				catch (Exception ex)
				{
					Debug.LogWarning("ProjScan detected an error during compilation of one shader: " + ex.ToString());
				}
			}
		}
	}
}
